package com.financial.sys.service.system.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.financial.common.constants.ResultCodeEnum;
import com.financial.common.exception.TsException;
import com.financial.model.pojo.TSystemDictionary;
import com.financial.sys.mapper.system.TSystemDictionaryMapper;
import com.financial.sys.service.system.TSystemDictionaryService;
import com.financial.sys.vo.repayment.TSystemDictionaryQuery;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hzy
 * @since 2020-02-12
 */
@Service
public class TSystemDictionaryServiceImpl extends ServiceImpl<TSystemDictionaryMapper, TSystemDictionary> implements TSystemDictionaryService {

	@Override
	public Map<String, Object> query(TSystemDictionaryQuery tSystemDictionaryQuery) {
		if (StringUtils.isEmpty(tSystemDictionaryQuery)) {
			throw new TsException(ResultCodeEnum.Null_Object);
		}
		QueryWrapper queryWrapper = new QueryWrapper();
		if (tSystemDictionaryQuery.getKeyword() != null && tSystemDictionaryQuery.getKeyword() != "") {
			queryWrapper.like("name", tSystemDictionaryQuery.getKeyword());
		}
		IPage ipage = new Page(tSystemDictionaryQuery.getCurrentPage(), tSystemDictionaryQuery.getPageSize());
		IPage page = baseMapper.selectPage(ipage, queryWrapper);
		Map<String, Object> map = new HashMap<>();

		map.put("currentPage", page.getCurrent());
		map.put("pageSize", page.getSize());
		//是否存在上一页
		if (page.getCurrent() > 1L) {
			map.put("prevPage", page.getCurrent() - 1);
		} else {
			map.put("prevPage", page.getCurrent());
		}
		//是否存在下一页
		if (page.getCurrent() < page.getPages()) {
			map.put("nextPage", page.getCurrent() + 1);
		} else {
			map.put("nextPage", page.getCurrent());
		}
		map.put("totalCount", page.getTotal());
		map.put("totalPage", page.getPages());
		map.put("listData", page.getRecords());
		return map;
	}
}
